//#include "Worksheet.h"
#include "AddNodeDialog.h"
#include "SendForm.h"
#include "AboutForm.h"
#include "Keylistner.h"
#include "SaveTo.h"
#include "ReadFrom.h"
#include "Paste.h"
#include "GetFile.h"

//pawel 13.03.2007 od
#include "Root.h"
#include "Node.h"
//pawel 13.03.2007 do
#pragma once


int DefaultEllipseWidth=30;
int DefaultEllipseHeight=15;

char *nazwa="";


int posH=0,posV=0;
int mouseRightButton=0;
int mouseLeftButton=0;		//14.04.07 Pawel
System::Drawing::Point temporaryMousePosition;

void paintTree(Node *ws,bool root, System::Drawing::Graphics^ screen)
{
	Node *tmp;
	if(!root)
	{
		screen->DrawLine(gcnew System::Drawing::Pen(System::Drawing::Color::Black),ws->getFather()->getX()+ws->getFather()->getWidth()-posH,ws->getFather()->getY()-posV+15,ws->getFather()->getX()+ws->getFather()->getWidth()-posH+15,ws->getFather()->getY()-posV+15);
		screen->DrawLine(gcnew System::Drawing::Pen(System::Drawing::Color::Black),ws->getFather()->getX()+ws->getFather()->getWidth()-posH+15,ws->getFather()->getY()-posV+15,ws->getFather()->getX()+ws->getFather()->getWidth()-posH+15,ws->getY()-posV+15);
		screen->DrawLine(gcnew System::Drawing::Pen(System::Drawing::Color::Black),ws->getFather()->getX()+ws->getFather()->getWidth()-posH+15,ws->getY()-posV+15,ws->getX()-posH,ws->getY()-posV+15);
	}
	if(ws->getVisible())
	{

		tmp=ws->first;
		while(tmp)
		{
			paintTree(tmp,false,screen);
			tmp=tmp->getNext();
		}
	}/* rembol - to zmienilem, 27.03 */ else if (ws->first!=NULL)
	{
		screen->DrawLine(gcnew System::Drawing::Pen(System::Drawing::Color::Black),ws->getX()+ws->getWidth()-posH,ws->getY()-posV+15,ws->getX()+ws->getWidth()-posH+15,ws->getY()-posV+15);
		screen->FillEllipse(gcnew SolidBrush(Color::Red),RectangleF(ws->getX()+ws->getWidth()-posH+15,ws->getY()-posV+10,10,10));
	}/* dotad */
	System::Drawing::Pen^ ellipsePen=gcnew System::Drawing::Pen(System::Drawing::Color::Black);
	ellipsePen->Width=1;
	if (root)
	{
		System::Drawing::Rectangle ellipseRect(ws->getX()-10-posH,ws->getY()+10-posV,ws->getWidth()+10,2*DefaultEllipseHeight);
		System::Drawing::Rectangle ellipseRect2(ws->getX()-posH,ws->getY()-posV,ws->getWidth()+10,2*DefaultEllipseHeight);
		screen->FillRectangle(gcnew SolidBrush(Color::LightGray),ellipseRect);
		screen->FillRectangle(gcnew SolidBrush(Color::White),ellipseRect2);
		screen->DrawRectangle(ellipsePen,ellipseRect2);
	}
	if (ws->getFocus()) ellipsePen->Width=3;
	String^ text=gcnew System::String(ws->getNodeName().c_str());
	if (!root /* rembol, 27.03 */|| ws->getFocus()/* dotad */)screen->DrawLine(ellipsePen,ws->getX()-posH,ws->getY()-posV+20,ws->getX()+ws->getWidth()-posH,ws->getY()+20-posV);
		
	screen->DrawString(text,gcnew Font("courier new",10),gcnew SolidBrush(Color::Black),ws->getX()-posH,ws->getY()+6-posV);

}

namespace RedDot {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

		/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Keylistner *k;
		System::Drawing::Graphics^ temporaryGraphics;
/*Pawel 13.03.07*/ Root *tree;

	private: System::Windows::Forms::ImageList^  imageList1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::ToolStripMenuItem^  helpToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  manualToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutProgramToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  wyslijNaSerwerToolStripMenuItem;
	private: System::Windows::Forms::Panel^  panel1;
	public: 
		Form1(void)
		{
			InitializeComponent();
//			currentWorksheet->addRDobject(0,500,350," Map1");
			tree=new Root("Map1",500,350,0);
			
			k=(Keylistner*)new Keylistner(tree);
	
			
			
		}
		Form1(String^ name)
		{
			InitializeComponent();
            char *bufor = (char*)(void*)Marshal::StringToHGlobalAnsi(name);
//			currentWorksheet->addRDobject(0,500,350," Map1");
			tree=new Root("Map1",500,350,0);
			tree=ReadFrom(bufor,tree);
			k=(Keylistner*)new Keylistner(tree);
			
	
//			
			
			
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	protected: 
	private: System::Windows::Forms::ToolStripMenuItem^  toolStripMenuItem1;
	private: System::Windows::Forms::ToolStripMenuItem^  toolStripMenuItem2;
	private: System::Windows::Forms::ToolStripMenuItem^  toolStripMenuItem3;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  editToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  copyToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  pasteToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  saveToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  saveAsToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  cutToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  deleteToolStripMenuItem;
















	private: System::ComponentModel::IContainer^  components;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->toolStripMenuItem2 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveAsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->toolStripMenuItem3 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->wyslijNaSerwerToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->editToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->copyToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->pasteToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->cutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->deleteToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->manualToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutProgramToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->imageList1 = (gcnew System::Windows::Forms::ImageList(this->components));
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->menuStrip1->SuspendLayout();
			this->panel1->SuspendLayout();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->toolStripMenuItem1, 
				this->editToolStripMenuItem, this->helpToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(643, 24);
			this->menuStrip1->TabIndex = 0;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// toolStripMenuItem1
			// 
			this->toolStripMenuItem1->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(6) {this->toolStripMenuItem2, 
				this->saveToolStripMenuItem, this->saveAsToolStripMenuItem, this->toolStripMenuItem3, this->wyslijNaSerwerToolStripMenuItem, 
				this->exitToolStripMenuItem});
			this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
			this->toolStripMenuItem1->Size = System::Drawing::Size(35, 20);
			this->toolStripMenuItem1->Text = L"File";
			// 
			// toolStripMenuItem2
			// 
			this->toolStripMenuItem2->Name = L"toolStripMenuItem2";
			this->toolStripMenuItem2->Size = System::Drawing::Size(164, 22);
			this->toolStripMenuItem2->Text = L"New";
			this->toolStripMenuItem2->Click += gcnew System::EventHandler(this, &Form1::toolStripMenuItem2_Click);
			// 
			// saveToolStripMenuItem
			// 
			this->saveToolStripMenuItem->Name = L"saveToolStripMenuItem";
			this->saveToolStripMenuItem->Size = System::Drawing::Size(164, 22);
			this->saveToolStripMenuItem->Text = L"Save";
			this->saveToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::saveToolStripMenuItem_Click);
			// 
			// saveAsToolStripMenuItem
			// 
			this->saveAsToolStripMenuItem->Name = L"saveAsToolStripMenuItem";
			this->saveAsToolStripMenuItem->Size = System::Drawing::Size(164, 22);
			this->saveAsToolStripMenuItem->Text = L"Save as...";
			this->saveAsToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::saveAsToolStripMenuItem_Click);
			// 
			// toolStripMenuItem3
			// 
			this->toolStripMenuItem3->Name = L"toolStripMenuItem3";
			this->toolStripMenuItem3->Size = System::Drawing::Size(164, 22);
			this->toolStripMenuItem3->Text = L"Open";
			this->toolStripMenuItem3->Click += gcnew System::EventHandler(this, &Form1::toolStripMenuItem3_Click);
			// 
			// wyslijNaSerwerToolStripMenuItem
			// 
			this->wyslijNaSerwerToolStripMenuItem->Name = L"wyslijNaSerwerToolStripMenuItem";
			this->wyslijNaSerwerToolStripMenuItem->Size = System::Drawing::Size(164, 22);
			this->wyslijNaSerwerToolStripMenuItem->Text = L"Wyslij na serwer";
			this->wyslijNaSerwerToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::wyslijNaSerwerToolStripMenuItem_Click);
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->ShortcutKeyDisplayString = L"Alt F4";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(164, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::exitToolStripMenuItem_Click);
			// 
			// editToolStripMenuItem
			// 
			this->editToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->copyToolStripMenuItem, 
				this->pasteToolStripMenuItem, this->cutToolStripMenuItem, this->deleteToolStripMenuItem});
			this->editToolStripMenuItem->Name = L"editToolStripMenuItem";
			this->editToolStripMenuItem->Size = System::Drawing::Size(37, 20);
			this->editToolStripMenuItem->Text = L"Edit";
			// 
			// copyToolStripMenuItem
			// 
			this->copyToolStripMenuItem->Name = L"copyToolStripMenuItem";
			this->copyToolStripMenuItem->Size = System::Drawing::Size(116, 22);
			this->copyToolStripMenuItem->Text = L"Copy";
			this->copyToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::copyToolStripMenuItem_Click);
			// 
			// pasteToolStripMenuItem
			// 
			this->pasteToolStripMenuItem->Name = L"pasteToolStripMenuItem";
			this->pasteToolStripMenuItem->Size = System::Drawing::Size(116, 22);
			this->pasteToolStripMenuItem->Text = L"Paste";
			this->pasteToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::pasteToolStripMenuItem_Click);
			// 
			// cutToolStripMenuItem
			// 
			this->cutToolStripMenuItem->Name = L"cutToolStripMenuItem";
			this->cutToolStripMenuItem->Size = System::Drawing::Size(116, 22);
			this->cutToolStripMenuItem->Text = L"Cut";
			this->cutToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::cutToolStripMenuItem_Click);
			// 
			// deleteToolStripMenuItem
			// 
			this->deleteToolStripMenuItem->Name = L"deleteToolStripMenuItem";
			this->deleteToolStripMenuItem->Size = System::Drawing::Size(116, 22);
			this->deleteToolStripMenuItem->Text = L"Delete";
			this->deleteToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::deleteStripMenuItem_Click);
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->manualToolStripMenuItem, 
				this->aboutProgramToolStripMenuItem});
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			// 
			// manualToolStripMenuItem
			// 
			this->manualToolStripMenuItem->Name = L"manualToolStripMenuItem";
			this->manualToolStripMenuItem->Size = System::Drawing::Size(157, 22);
			this->manualToolStripMenuItem->Text = L"Manual";
			this->manualToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::manualToolStripMenuItem_Click);
			// 
			// aboutProgramToolStripMenuItem
			// 
			this->aboutProgramToolStripMenuItem->Name = L"aboutProgramToolStripMenuItem";
			this->aboutProgramToolStripMenuItem->Size = System::Drawing::Size(157, 22);
			this->aboutProgramToolStripMenuItem->Text = L"About Program";
			this->aboutProgramToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::aboutProgramToolStripMenuItem_Click);
			// 
			// imageList1
			// 
			this->imageList1->ColorDepth = System::Windows::Forms::ColorDepth::Depth8Bit;
			this->imageList1->ImageSize = System::Drawing::Size(16, 16);
			this->imageList1->TransparentColor = System::Drawing::Color::Transparent;
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(194, 64);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(124, 20);
			this->textBox1->TabIndex = 0;
			this->textBox1->Visible = false;
			this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown);
			// 
			// panel1
			// 
			this->panel1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->panel1->Controls->Add(this->textBox1);
			this->panel1->Location = System::Drawing::Point(0, 27);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(643, 467);
			this->panel1->TabIndex = 1;
			this->panel1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::panel1_MouseDown);
			this->panel1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::panel1_MouseMove);
			this->panel1->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::panel1_Paint);
			this->panel1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::panel1_MouseUp);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::White;
			this->ClientSize = System::Drawing::Size(643, 494);
			this->Controls->Add(this->panel1);
			this->Controls->Add(this->menuStrip1);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"Red Dot";
			this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
			this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing);
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->panel1->ResumeLayout(false);
			this->panel1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void toolStripMenuItem3_Click(System::Object^  sender, System::EventArgs^  e) {
			 
			 OpenFileDialog ^OFD=gcnew OpenFileDialog();
			 OFD->Filter="RedDot Files (*.rdt)|*.rdt|All Files (*.*)|*.*";
			 OFD->FilterIndex=1;
			
			 OFD->ShowDialog();
			 char *bufor="";
			 bufor = (char*)(void*)Marshal::StringToHGlobalAnsi(OFD->FileName);
			 
			 if(strlen(bufor)!=0)
				 nazwa=bufor;
			 if(strlen(bufor)!=0)
			 tree=ReadFrom(nazwa,tree);
			 if(strlen(nazwa)!=0){
				 
				 this->Text=gcnew System::String(nazwa);
			 }
			 tree->focus=1;
			 posH=0;
			 posV=0;

			
			 Keylistner *j=(Keylistner*)new Keylistner(tree);
			 free(k);
			 k=j;
			 panel1->CreateGraphics()->Clear(Color::White);
/*Pawel 13.03.07*/		 paintTree(tree,true,panel1->CreateGraphics());
			
			 
			 
			 
			 
			 }
	private: System::Void asToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			this->Close();
			 
		 }
		
private: System::Void nodeToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			
		 }

private: System::Void pictureBox1_PaintpictureBox1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {	

		 }
private: System::Void pictureBox1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 Graphics ^screen = panel1->CreateGraphics();
			 System::Windows::Forms::KeyEventArgs^ x;
			 x=e;
			 this->Focus();
			 k->KeyDown(e);
			 if(k->tField) {
		    
			
			
//			 
		textBox1->Location = System::Drawing::Point( k->tF_X-posH, k->tF_Y-posV);
			textBox1->Name = L"textBox1";
			textBox1->Size = System::Drawing::Size(124, 20);
			textBox1->TabIndex = 0;
			textBox1->Visible = true;
			textBox1->Focus();
			k->tField = false;
			 }

			 if (k->wezel->getX()-posH<30)
				 posH=k->wezel->getX()-30;
			 if (k->wezel->getX()+k->wezel->getWidth()-posH>panel1->Width-30)
				 posH=k->wezel->getX()+k->wezel->getWidth()-panel1->Width+30;
			 if (k->wezel->getY()-posV<30)
				 posV=k->wezel->getY()-30;
			 if (k->wezel->getY()+k->wezel->getHeight()-posV>panel1->Height-30)
				 posV=k->wezel->getY()+k->wezel->getHeight()-panel1->Height+30;

			 screen->Clear(Color::White);
/*Pawel 13.03.07*/			 paintTree(tree,true,screen);
		 }
private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
		 	 
		 }
private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
		 panel1->CreateGraphics()->Clear(Color::White);
/*Pawel 13.03.07*/		 paintTree(tree,true,panel1->CreateGraphics());


		 }
//Bury 10.04.07 od
/*funkcja panel1_MouseDown to obsluga myszy, w przypadku klikniecia mniej wiecej na noda 
ustawia go jako focus, jesli node przed kliknieciem byl juz focusem to otwiera okno edycji
no i tak rozwiazalem kwestie podwojnego klikniecia na noda - jezeli komus nie pasi to sie zmieni
ale jak dla mnie jest fajnie :P*/
private: System::Void panel1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			 if (e->Button==System::Windows::Forms::MouseButtons::Right)
			 {
				 mouseRightButton=1;
				 temporaryMousePosition.X=e->X;
				 temporaryMousePosition.Y=e->Y;
			 }
 
			 if (e->Button==System::Windows::Forms::MouseButtons::Left)
			 {
				 temporaryMousePosition.X=e->X;
				 temporaryMousePosition.Y=e->Y;

				 Node *wezel=k->getWezel();

				 Node *tmp=wezel;
				 if((tmp==tree->check(tree, (temporaryMousePosition.X+posH), (temporaryMousePosition.Y+posV) )))
				//jezeli klikamy na focus to edycja
				 {
					 k->editNode(wezel);
				 }
				 else if((tmp=tree->check(tree, (temporaryMousePosition.X+posH), (temporaryMousePosition.Y+posV) )))
				//jezeli node nie byl focus to teraz juz jest :P
				{
					wezel->focus=false;
					wezel=tmp;
					k->setWezel(wezel);
					mouseLeftButton=1; 
				}
				panel1->Refresh();

			 }
			
		 }
//Bury 10.04.07 do
private: System::Void panel1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
		 if (e->Button==System::Windows::Forms::MouseButtons::Right)
			 {
				 mouseRightButton=0;
			 }
		  mouseLeftButton=0;

		 }
private: System::Void panel1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			if (mouseRightButton)
			{
				posH-=e->X-temporaryMousePosition.X;
				posV-=e->Y-temporaryMousePosition.Y;
				temporaryMousePosition.X=e->X;
				temporaryMousePosition.Y=e->Y;
				panel1->Refresh();
			}
			if(mouseLeftButton)
			{
				k->getWezel()->move(e->X-temporaryMousePosition.X,temporaryMousePosition.Y-e->Y);
				temporaryMousePosition.X=e->X;
				temporaryMousePosition.Y=e->Y;
				panel1->Refresh();
			}
			
		 }
private: System::Void saveAsToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
/*			 rdObject *obiekt=currentWorksheet->getObjects();
			 Node *n;
			 n=(Node*)obiekt->WSelement;*/
			 SaveFileDialog ^SFD=gcnew SaveFileDialog;
			
			 SFD->Filter="RedDot Files (*.rdt)|*.rdt|All Files (*.*)|*.*";
			 SFD->FilterIndex=1;
			 SFD->ShowDialog();
			 char *bufor="";
			 bufor = (char*)(void*)Marshal::StringToHGlobalAnsi(SFD->FileName);
			 if(strlen(bufor)!=0)
				 nazwa=bufor;

			 
//			 SaveTo(nazwa,n,NULL);
			 if(strlen(bufor)!=0)
			 SaveTo(nazwa,(Node*)tree,NULL);
			 if(strlen(nazwa)!=0){
				 this->Text=gcnew System::String(nazwa);
			 }
		 }
private: System::Void saveToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
/*			 rdObject *obiekt=currentWorksheet->getObjects();
			 Node *n;
			 n=(Node*)obiekt->WSelement;*/
			 if(strlen(nazwa)==0){
				SaveFileDialog ^SFD=gcnew SaveFileDialog;
				SFD->Filter="RedDot Files (*.rdt)|*.rdt|All Files (*.*)|*.*";
			    SFD->FilterIndex=1;
			 
				SFD->ShowDialog();
				char *bufor="";
			 bufor = (char*)(void*)Marshal::StringToHGlobalAnsi(SFD->FileName);
			 if(strlen(bufor)!=0)
				 nazwa=bufor;

				
			 }
//			 SaveTo(nazwa,n,NULL);
			 SaveTo(nazwa,(Node*)tree,NULL);
			 if(strlen(nazwa)!=0){
				 this->Text=gcnew System::String(nazwa);
			 }
				 
			
		 }
		
private: System::Void textBox1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 if(e->KeyCode==Windows::Forms::Keys::Enter) {
				 char* str = (char*)(void*)Marshal::StringToHGlobalAnsi(textBox1->Text);
				 k->edited->changeNodeName(str);
				 textBox1->Visible = false;
		
				 this->Focus();
				 //panel1->Refresh();
				 k->setWezel(k->edited);
				 textBox1->Text="";
			//	if(k->wezel->father) k->wezel=k->wezel->next;
			//		else k->wezel=k->wezel->last;


		 }


		 
};
private: System::Void deleteStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e){
			
			 Node *tmp=k->wezel;
				if(k->wezel->father)
				{
					k->wezel=k->wezel->remove();
					delete tmp;
				}
				panel1->CreateGraphics()->Clear(Color::White);
				 paintTree(tree,true,panel1->CreateGraphics());
		 };
		 
//toolStripMenuItem2
private: System::Void toolStripMenuItem2_Click(System::Object^  sender, System::EventArgs^  e){
			 delete tree;
			 tree=new Root("Map1",500,350,0);
			 panel1->CreateGraphics()->Clear(Color::White);
			 paintTree(tree,true,panel1->CreateGraphics());
			 k=new Keylistner(tree);
			 posH=0;
			 posV=0;
			 nazwa="";
			 this->Text=gcnew System::String("Red Dot");
			 
		 }

private: System::Void aboutProgramToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 AboutForm^ aboutForm =gcnew AboutForm();
			 aboutForm->ShowDialog();
		 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void copyToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 SaveTo("C:/Documents and Settings/RedDot/tmp.rdt",k->wezel,NULL);
		 }
private: System::Void pasteToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 //k->wezel->addChild("name",ReadFrom("C:/Documents and Settings/All Users/Dane aplikacji/RedDot/tmp.rdt",NULL));
			 Node *ble=new Node("nazwa",0);
			 ble=ReadFrom("C:/Documents and Settings/RedDot/tmp.rdt",NULL);
			 ble->setInvisible();
			 Paste(ble,k->wezel);
			 ble->setVisible();
			 k->wezel=ble;
			 if(k->wezel->previous) k->wezel->previous->focus=false;
			 k->wezel->focus=1;
			 panel1->CreateGraphics()->Clear(Color::White);
				 paintTree(tree,true,panel1->CreateGraphics());
			 
				 
		 }
private: System::Void cutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 SaveTo("C:/Documents and Settings/RedDot/tmp.rdt",k->wezel,NULL);
			 Node *tmp=k->wezel;
				if(k->wezel->father)
				{
					k->wezel=k->wezel->remove();
					delete tmp;
				}
				panel1->CreateGraphics()->Clear(Color::White);
				 paintTree(tree,true,panel1->CreateGraphics());
		 }
private: System::Void manualToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
	//		 system("hh \"c:/Documents and Settings/RedDot/reddot.chm\" ");
			 WinExec("hh \"c:/Documents and Settings/RedDot/reddot.chm\" ",SW_SHOW);
			 
		 }
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
			 WinExec("iexplore http://maephisto.no-ip.info/viewtopic.php?t=37",SW_SHOW);
		 }
private: System::Void wyslijNaSerwerToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 SendForm^ sendForm =gcnew SendForm();
			 sendForm->ShowDialog();
		 }
private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {
			 WinExec("iexplore http://maephisto.no-ip.info/viewtopic.php?t=37",SW_SHOW);
		 }
};
};